<?php
class SellerAction extends CommonAction {
    public function _initialize()
    {
        parent::_initialize();
    }
    function index(){
        $pagesize = 10;
        if ($this->_get('pagesize')) {
            $pagesize = $this->_get('pagesize');
        }
        //实例化分页类
        import('ORG.Util.Page');
        $listmap = 's.site_id = 143 ';
        $myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
        if($_GET['type']==0){//全部商家
            $mybelong=D('Member_connect')->getSeller($myid);
        }elseif($_GET['type']==1){//直属商家
            $mybelong=D('Member_connect')->myseller($myid);
        }elseif($_GET['type']==2){//业务员商家
            $mybelong=D('Member_connect')->secSeller($myid);
        }elseif($_GET['type']==3){//服务商商家
            $mybelong=D('Member_connect')->serverSeller($myid);
        }
        if(count($mybelong)>0){
            $listmap .= ' and s.id  in ('.implode(',',$mybelong).')';
        }else{
            $listmap .= ' and 1 = 0 ';
        }
        $p = new Page(count($mybelong), $pagesize);
        if($_GET['keywords']!=''){
            $myshop='( susername like "%'.$_GET['keywords'].'%" or contacts like "%'.$_GET['keywords'].'%" )';
        }else{
            $myshop = '';
        }
        $shops=D('Supplier')->where($myshop)->getfield('id,member_id');
        if(count($shops)>0){
            $firstlist = D('Member')->alias('s')->where($listmap)-> Field("s.*") -> select();
            $memberlist=[];
            foreach($firstlist as $key => $val){
                if(in_array($val['id'],$shops)){
                    array_push($memberlist,$val['id']);
                }
            }
            if(count($memberlist)>0){
                if(count($memberlist)==1){
                    $where='id = '.$memberlist[0];
                }else{
                    $where='id in ('.implode(',',$memberlist).')';
                }
                $list=D('Member')->where($where)->limit($p->firstRow . ',' . $p->listRows)-> Field("*") -> select();
                foreach($list as $k =>$v){
                    $shop=D('Supplier')->where('member_id = '.$v['id'])->field('susername,contacts,sphone')->find();
                    $list[$k]['shop']=$shop['susername'];
                    $list[$k]['contacts']=$shop['contacts'];
                    $list[$k]['sphone']=$shop['sphone'];
                    $list[$k]['money']=D('Shop_turnover')->where('shop_id = '.$v['id'])->sum('total');
                    $list[$k]['discount']=D('Member_account_log')->where('from_member = '.$v['id'].' and types = 2')->sum('changenum');
                    $list[$k]['discounted']=D('Member_account_log')->where('from_member = '.$v['id'].' and types = 2 and status = 1')->sum('changenum');
                    $list[$k]['discounting']=$list[$k]['discount']-$list[$k]['discounted'];
                }
            }
        }else{
            $list='';
        }
        $pages = $p->show();
        $this->assign('list', $list);
        $this->assign('pages', $pages);
        $this->display();
    }
    public function add(){
//        $myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
//        D('Member_connect')->getSeller($myid);
    }

}